﻿/*******************************************************************************
#                    OnlineIndexBroker.cs
#  Comment:
#
#  Current Version: V1.0
#  Author: 
#
#  History List:
#  V1.0    Created by Onions@2009-8-18 20:07:18
#
#******************************************************************************/
#region Usings
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using SYS.Basic;

using ExpertMIS.Brokers;
using ExpertMIS.Entities;
using System.Collections;
using IBatisNet.Extend;
#endregion

namespace ExpertMIS.Brokers
{
    public class OnlineIndexBroker : ExpertMISBaseBroker
    {
        #region Private Methods

        private object UpdateOnlineIndexEntity(IEntity entity)
        {
            return SqlMap.Update("UpdateOnlineIndexEntity", entity);
        }

        private object InsertOnlineIndexEntity(IEntity entity)
        {
            return SqlMap.Insert("InsertOnlineIndexEntity", entity);
        }

        private object DeleteOnlineIndexEntity(IEntity entity)
        {
            return SqlMap.Delete("DeleteOnlineIndexEntity", entity);
        }

        #endregion

        #region Override Methods

        protected override IEntity ProcessGetEntity(System.Collections.Hashtable keys)
        {
			if (!keys.Contains("OID")) throw new Exception("invalid request parameters.");
            return SqlMap.QueryForObject<IEntity>("SelectOnlineIndexEntityByOID", keys["OID"] as string);
        }

        protected override bool ProcessSaveEntity(IEntity entity, Hashtable request)
        {
            if (entity.EntityState == EntityState.New)
            {
                InsertOnlineIndexEntity(entity);
                entity.EntityState = EntityState.Old;
            }
            else
            {
                UpdateOnlineIndexEntity(entity);
            }

            return true;
        }

        protected override IEntity ProcessNewEntity()
        {
            OnlineIndexEntity entity = new OnlineIndexEntity();
			
			// initial entity object

            return entity;
        }

        protected override bool ProcessRemoveEntity(IEntity entity, Hashtable request)
        {
            try
            {
                DeleteOnlineIndexEntity(entity);
                return true;
            }
            catch
            {
                return false;
            }
        }

        #endregion

        #region Public Method
        public DataTable GetOptionView()
        {
            string sql = @"SELECT CASE 
                WHEN F_Option = '1' THEN '一般'
                WHEN F_Option = '2' THEN '很好'
                WHEN F_Option = '3' THEN '非常好'
                ELSE '其它'
                END AS F_Option, COUNT(*) AS F_Count
                FROM T_QUE_OnlineIndex
                Group by 
                CASE 
                WHEN F_Option = '1' THEN '一般'
                WHEN F_Option = '2' THEN '很好'
                WHEN F_Option = '3' THEN '非常好'
                ELSE '其它'
                END ORDER BY F_Count DESC";
            return SYS.Common.Broker.SQLBuilderBroker.GetSQLBuilderBroker().GetQueryView(sql);
        }
        #endregion
    }
}